package com.mqqzt.douban;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.douban.NoteEntry;
import com.mqqzt.douban.domain.Note;

public class NewNoteActivity extends BaseActivity implements OnClickListener {
	private EditText mEditTextTitle;
	private EditText mEditTextContent;
	private RadioButton rb_private, rb_friend, rb_public;
	private CheckBox cb_can_reply;
	private Button btnSave, btnCancel;
	private ProgressDialog pd;
	private boolean flag;
	private Note note;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.note_edit);
		super.onCreate(savedInstanceState);
		pd = new ProgressDialog(this);
		flag = getIntent().getBooleanExtra("iseditnote", false);
		if(flag){
			MyApp myApp = (MyApp) getApplication();
			note = myApp.note;
			mEditTextTitle.setText(note.getTitle());
			mEditTextContent.setText(note.getContent());
			String privacy = note.getPrivacy();
			if("private".equals(privacy)){
				rb_private.setChecked(true);
			}else if("friend".equals(privacy)){
				rb_friend.setChecked(true);
			}else if("public".equals(privacy)){
				rb_public.setChecked(true);
			}
			String can_reply = note.getCan_reply();
			if("yes".equals(can_reply)){
				cb_can_reply.setChecked(true);
			}else{
				cb_can_reply.setChecked(false);
			}
		}
	}

	@Override
	public void setupView() {
		mEditTextTitle = (EditText) findViewById(R.id.EditTextTitle);
		mEditTextContent = (EditText) findViewById(R.id.EditTextContent);
		rb_private = (RadioButton) this.findViewById(R.id.rb_private);
		rb_friend = (RadioButton) this.findViewById(R.id.rb_friend);
		rb_public = (RadioButton) this.findViewById(R.id.rb_public);
		cb_can_reply = (CheckBox) this.findViewById(R.id.cb_can_reply);
		btnSave = (Button) this.findViewById(R.id.btnSave);
		btnCancel = (Button) this.findViewById(R.id.btnCancel);
	}

	@Override
	public void setListener() {
		btnSave.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void fillData() {
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSave:
			final String title = mEditTextTitle.getText().toString().trim();
			final String content = mEditTextContent.getText().toString().trim();
			if("".equals(title)&&"".equals(content)){
				Toast.makeText(NewNoteActivity.this, "保存内容不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			String auth = null;
			if(rb_private.isChecked()){
				auth = "private";
			}else if(rb_friend.isChecked()){
				auth = "friend";
			}else if(rb_public.isChecked()){
				auth = "public";
			}
			String can_reply = null;
			if(cb_can_reply.isChecked()){
				can_reply = "yes";
			}else{
				can_reply = "no";
			}
			new AsyncTask<String, Void, Boolean>(){
				
				@Override
				protected void onPreExecute() {
					pd.setMessage("正在保存日记");
					pd.show();
					super.onPreExecute();
				}

				@Override
				protected void onPostExecute(Boolean result) {
					pd.dismiss();
					if(result){
						setResult(200);
						finish();
					}else{
						Toast.makeText(NewNoteActivity.this, "发表日记失败", Toast.LENGTH_SHORT).show();
					}
					super.onPostExecute(result);
				}

				@Override
				protected Boolean doInBackground(String... params) {
					try {
						if(flag){
							NoteEntry ne = note.getNoteEntry();
							myService.updateNote(ne, new PlainTextConstruct(title), new PlainTextConstruct(content), params[0], params[1]);
						}else{
						myService.createNote(new PlainTextConstruct(title), new PlainTextConstruct(content), params[0], params[1]);
						}
						return true;
					} catch (Exception e) {
						return false;
					}
				}
				
			}.execute(auth, can_reply);
			break;
		case R.id.btnCancel:
			finish();
			break;
		}

	}

}
